﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace OpenPhotoAlbum.Maintainance
{
	public partial class Photos : AlbumPage
	{
		protected override void AppendSiteMapNodes(OpenPhotoAlbum.Controls.AlbumSiteMapProvider provider)
		{
			base.AppendSiteMapNodes(provider);

			provider.AppendSiteMapNode("Maintainance Console", "~/Maintainance/Default.aspx");
			provider.AppendSiteMapNode("Latest Uploaded Photos", RawUrl);
		}

		protected override void OnInit(EventArgs e)
		{
			base.OnInit(e);

			GridView1.RowDataBound += new GridViewRowEventHandler(GridView1_RowDataBound);
			GridView1.RowCommand += new GridViewCommandEventHandler(GridView1_RowCommand);
		}

		void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
		{
			if (e.CommandName == "BanUnban")
			{
				DataKey key = GridView1.DataKeys[int.Parse((string)e.CommandArgument)];
				Guid photoId = (Guid)key.Value;
				BusinessLogics.PhotoManager.Instance.BanUnbanPhoto(photoId);
				GridView1.DataBind();
			}
			else if (e.CommandName == "DeletePhoto")
			{
				DataKey key = GridView1.DataKeys[int.Parse((string)e.CommandArgument)];
				Guid photoId = (Guid)key.Value;
				BusinessLogics.PhotoManager.Instance.DeletePhoto(photoId);
				GridView1.DataBind();
			}
			else if (e.CommandName == "PromoteDemote")
			{
				DataKey key = GridView1.DataKeys[int.Parse((string)e.CommandArgument)];
				Guid photoId = (Guid)key.Value;
				BusinessLogics.PhotoManager.Instance.PromoteDemotePhoto(photoId);
				GridView1.DataBind();
			}
		}

		void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
		{
			if (e.Row.RowType != DataControlRowType.DataRow) { return; }

			DbAccess.Photo photo = (DbAccess.Photo)e.Row.DataItem;

			HyperLink hlAlbum = (HyperLink)e.Row.FindControl("hlAlbum");
			hlAlbum.Text = HttpUtility.HtmlEncode(photo.Album.AlbumTitle);
			hlAlbum.NavigateUrl = UrlContext.BuildSharedAlbumUrl(photo.AlbumId);

			HyperLink hlUser = (HyperLink)e.Row.FindControl("hlUser");
			hlUser.Text = HttpUtility.HtmlEncode(photo.Album.User.UserName);
			hlUser.NavigateUrl = UrlContext.BuildSharedHomeUrl(photo.Album.User.UserName);

			HyperLink hlPhoto = (HyperLink)e.Row.FindControl("hlPhoto");
			hlPhoto.NavigateUrl = UrlContext.BuildSharedPhotoUrl(photo.PhotoId);

			Image imgPhoto = (Image)e.Row.FindControl("imgPhoto");
			imgPhoto.ImageUrl = UrlContext.BuildPhotoPipeUrl(photo.PhotoId, BusinessLogics.PhotoTypes.Thumbnail);
		}

		public string BoolToSymbol(bool value)
		{
			if (value) { return "<b style=\"color:red;\">Yes</b>"; }
			else { return "No"; }
		}

		public string AlbumCoverToString(bool value)
		{
			if (value) { return "(Album Cover)"; }
			else { return ""; }
		}
	}
}